package com.pubinfo.passbook.api.controller.rule;

import com.alibaba.fastjson.JSONObject;
import com.pubinfo.passbook.api.eip.UserInfoService;
import com.pubinfo.passbook.common.model.ruler.PayInformation;
import com.pubinfo.passbook.common.model.ruler.PersonInformation;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.service.master.ITSourceArtificialService;
import com.pubinfo.passbook.common.service.ruler.CheckService;
import com.pubinfo.passbook.common.service.ruler.InformationService;
import com.pubinfo.passbook.common.service.ruler.RuleWenZhouService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping(value = "ruler")
@Api(tags = "审核规则")
@Slf4j
public class RulerController {
  
  @Autowired
  InformationService informationService;
  
  @Autowired
  UserInfoService userInfoService;
  @Autowired
  ITSourceArtificialService artificialService;
  @Autowired
  RuleWenZhouService wenZhouService;
  @Autowired
  CheckService checkService;
  
  @PostMapping("getXml/{id}")
  @ApiOperation(value = "获取票据xml中的信息")
  public PayInformation getXml(@PathVariable String id) throws DocumentException {
    return informationService.GetInformationFromPassbook(id);
  }
  
  @PostMapping("GetPersonInformationFrom4A/{id}")
  @ApiOperation(value = "根据账号获取用户信息")
  public PersonInformation GetPersonInformationFrom4A(@PathVariable String id) {
    return informationService.GetPersonInformationFrom4A(id);
  }
  
  @PostMapping("checkPassbookType")
  @ApiOperation(value = "上传的票据类型校验")
  public JsonResult<Boolean> checkPassbookType(@RequestBody List<String> idList) {
    return JsonResult.success(checkService.checkPassbookType(idList));
  }
  
  @PostMapping("checkPassbookPayer/{acct}")
  @ApiOperation(value = "上传的票据人员校验")
  public JsonResult<Boolean> checkPassbookPayer(@RequestBody List<String> idList, @PathVariable String acct) throws DocumentException {
    return JsonResult.success(checkService.checkPassbookPayer(idList, acct));
  }
  
  @PostMapping("testSum")
  @ApiOperation(value = "根据票据列表，校验该票据在所有地区可报销金额")
  public JsonResult<JSONObject> testSum(@RequestBody List<String> idList) {
    return JsonResult.success(wenZhouService.testSum(idList));
  }
  
  //  @PostMapping("sumAmountSelf")
//  @ApiOperation(value = "靠报销内容总自费金额")
//  public JsonResult<BigDecimal> sumAmountSelf(@RequestBody List<Details> detailsList) throws DocumentException {
//    return JsonResult.success(checkService.sumAmountSelf(detailsList));
//  }
//
  @PostMapping("checkUserPermission/{acct}")
  @ApiOperation(value = "检查用户是否有权限报销")
  public JsonResult<Boolean> checkUserPermission(@RequestBody List<String> invoiceIds, @PathVariable String acct) throws DocumentException {
  
    return JsonResult.success(checkService.checkUserPermission(invoiceIds, acct));
  }
  
  @PostMapping("getSumAmountSelfByType/{passbookId}")
  @ApiOperation(value = "根据票据id获取票据的某类自费总金额")
  public JsonResult<BigDecimal> getSumAmountSelfByType(@PathVariable String passbookId, @RequestBody String[] type) throws DocumentException {
    List<String> list = Arrays.asList(type);
    list.removeIf(Objects::isNull);
    Map<String, BigDecimal> types = new HashMap<>();
    if (list.size() > 0) {
      for (String s : list) {
        types.put(s, new BigDecimal("1"));
      }
    }
    return JsonResult.success(informationService.getSumAmountSelfByType(passbookId, types));
  }
  
}
